define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template', 'cookie'], function ($, undefined, Backend, Table, Form, Template, undefined) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();

           //绑定事件
           $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
            var panel = $($(this).attr("href"));
            if (panel.length > 0) {
                Controller.table[panel.attr("id")].call(this);
                $(this).on('click', function (e) {
                    $($(this).attr("href")).find(".btn-refresh").trigger("click");
                });
            }
            //移除绑定的事件
            $(this).unbind('shown.bs.tab');
        });

        //必须默认触发shown.bs.tab事件
        $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        table: {
            subject: function () {
                // 表格1
                var table1 = $("#table1");
                table1.bootstrapTable({
                    url: 'product/productrecycle/index',
                    extend: {
                        index_url: 'product/productrecycle/index',
                        del_url: 'product/productrecycle/del',
                        restore_url: 'product/productrecycle/restore',
                        table: 'product',
                    },
                    toolbar: '#toolbar1',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            { checkbox: true },
                            { field: 'id', title: __('Id'), sortable: true },
                            { field: 'name', title: __('Name'), operate: 'LIKE' },
                            {field: 'content', title: __('Content'), searchable: false ,visible:false },
                            { field: 'thumbs_text', title: __('Thumbs'), formatter: Table.api.formatter.image, searchable: false ,visible:false},
                            { field: 'price', title: __('Price') },
                            { field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: { "1": __('上架'), "0": __('下架')} },
                            { field: 'rent', title: __('Rent') },
                            { field: 'rent_price', title: __('RentPrice') },
                            { field: 'rentstatus', title: __('Rentstatus'), formatter: Table.api.formatter.label, searchList: { "1": __('普通商品'), "2": __('租赁商品只能租'), "3": __('既购买也租赁的商品') } },
                            { field: 'type.name', title: __('TypeName'), searchList: $.getJSON("product/product/type") },
                            { field: 'unit.name', title: __('UnitName'), searchList: $.getJSON("product/product/unit") },
                            { field: 'flag', title: __('Flag'), formatter: Table.api.formatter.label, searchList: { "1": __('新品'), "2": __('热销'), "3": __('推荐') } },
                            { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                            {
                                field: 'operate', title: __('Operate'), table: table1, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                                buttons: [
                                    {
                                        name: '商品还原',
                                        title: __('还原'),
                                        classname: 'btn btn-success btn-xs btn-ajax',
                                        icon: 'fa fa-reply',
                                        url: 'product/productrecycle/restore',
                                        extend: 'data-toggle="tooltip" data-container="body"',
                                        confirm: '确定还原吗',
                                        refresh: true, //操作完成后是否刷新列表
                                    }]
                            }
                        ]
                    ]
                });
                $(".subject").click(function () {
                    $ids = Table.api.selectedids(table1)
                    layer.confirm('确定还原' + $ids.length + "条数据吗?", { icon: 3, title: '提示' }, function (index) {
                        if (index) {
                            Backend.api.ajax("product/productrecycle/restore?ids=" + $ids.join(","), function (response) {
                                // 处理返回的结果
                                layer.close(index);
                                table1.bootstrapTable('refresh')
                               
                            })

                        }

                        layer.close(index);
                    });
                }),
                    // 为表格1绑定事件
                    Table.api.bindevent(table1);
            },
            order: function () {
                // 表格2
                var table2 = $("#table2");
                table2.bootstrapTable({
                    url: 'product/ordrecycle/index',
                    extend: {
                        index_url: 'product/subrecycle/index',
                        del_url: 'product/ordrecycle/del',
                        restore_url: 'product/ordrecycle/restore',
                        table: 'order',
                    },
                    toolbar: '#toolbar2',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            { checkbox: true },
                            { field: 'id', title: __('Id'), sortable: true },
                            { field: 'code', title: __('Code') },
                            { field: 'business.nickname', title: __('BusName'), operate: 'LIKE' },
                            { field: 'amount', title: __('Amount'), operate: '>=' },
                            { field: 'expressquery.name', title: __('ExpressName'), operate: 'LIKE' },
                            { field: 'expresscode', title: __('Expresscode') },
                            { field: 'status', title: __('OStatus'), searchList: { "0": __('未支付'), "1": __('已支付'), "2": __('已发货'), "3": __('已收货'), "4": __('已完成'), "-1": __('仅退款'), "-2": __('退款退货'), "-3": __('售后中'), "-4": __('审核成功'), "-5": __('审核失败') }, formatter: Table.api.formatter.status },
                            { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                            {
                                field: 'operate', title: __('Operate'), table: table2, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                                buttons: [
                                    {
                                        name: '订单还原',
                                        title: __('还原'),
                                        classname: 'btn btn-success btn-xs btn-ajax',
                                        icon: 'fa fa-reply',
                                        url: 'product/ordrecycle/restore',
                                        extend: 'data-toggle="tooltip" data-container="body"',
                                        confirm: '确定还原吗',
                                        refresh: true, //操作完成后是否刷新列表
                                    }]
                            }
                        ]
                    ]
                });
                $(".subject").click(function () {
                    $ids = Table.api.selectedids(table2)
                    layer.confirm('确定还原' + $ids.length + "条数据吗?", { icon: 3, title: '提示' }, function (index) {
                        if (index) {
                            Backend.api.ajax("product/ordrecycle/restore?ids=" + $ids.join(","), function (response) {
                                // 处理返回的结果
                                layer.close(index);
                                // 刷新数据表格
                                table2.bootstrapTable('refresh')
                            })

                        }
                        layer.close(index);
                    });
                }),
                // 为表格2绑定事件
                Table.api.bindevent(table2);
            }
        },

        del: function () {
            Controller.api.bindevent();
        },
        restore: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});
